int strcmp ( const char * str1, const char * str2 );
| return value | indicates |
|---|---|
<0 | the first character that does not match has a lower value in ptr1 than in ptr2 |
0 | the contents of both strings are equal |
>0 | the first character that does not match has a greater value in ptr1 than in ptr2 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>
int main ()
{
char key[] = "apple";
char buffer[80];
do {
printf ("Guess my favorite fruit? ");
fflush (stdout);
scanf ("%79s",buffer);
} while (strcmp (key,buffer) != 0);
puts ("Correct answer!");
return 0;
}
Guess my favourite fruit? orange Guess my favourite fruit? apple Correct answer!